# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: team/kde <bribbers@disroot.org>

# The group tag is just to easily find this APKBUILD by some scripts for automation
# group=kde-plasma
pkgname=discover
pkgver=5.27.10
pkgrel=0
pkgdesc="KDE Plasma resources management GUI"
url="https://userbase.kde.org/Discover"
# armhf blocked by qt5-qtdeclarative
# ppc64le, riscv64 and s390x blocked by purpose
arch="all !armhf !ppc64le !riscv64 !s390x"
license="LGPL-2.0-or-later AND (GPL-2.0-only OR GPL-3.0-only) AND GFDL-1.2-only"
depends="kirigami2"
makedepends="
	appstream-dev
	attica5-dev
	extra-cmake-modules
	flatpak-dev
	karchive5-dev
	kauth5-dev
	kcmutils5-dev
	kconfig5-dev
	kcoreaddons5-dev
	kcrash5-dev
	kdbusaddons5-dev
	kdeclarative5-dev
	ki18n5-dev
	kidletime5-dev
	kio5-dev
	kitemmodels5-dev
	knewstuff5-dev
	kuserfeedback-dev
	kxmlgui5-dev
	libapk-qt-dev
	plasma-framework5-dev
	purpose5-dev
	qt5-qtbase-dev
	qt5-qtdeclarative-dev
	samurai
	"
checkdepends="xvfb-run"

# secfixes:
#   5.21.3-r0:
#     - CVE-2021-28117

case "$pkgver" in
	*.90*) _rel=unstable;;
	*) _rel=stable;;
esac
_repo_url="https://invent.kde.org/plasma/discover.git"
source="
	https://download.kde.org/$_rel/plasma/$pkgver/discover-$pkgver.tar.xz
	discover-apk-support.patch
	alpine-appstream-data.json
	alpine-linux-logo-icon.png
	"
subpackages="
	$pkgname-backend-apk:backend_apk
	$pkgname-backend-flatpak:backend_flatpak
	$pkgname-lang
	"

case "$CARCH" in
	x86|x86_64|armv7|aarch64)
		makedepends="$makedepends fwupd-dev"
		subpackages="$subpackages $pkgname-backend-fwupd:backend_fwupd"
		;;
	*) ;;
esac

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_DISABLE_FIND_PACKAGE_Snapd=ON \
		-DWITH_KCM=OFF
	cmake --build build
}

check() {
	cd build
	# knsbackendtest and flatpaktest fail to find their required executables
	CTEST_OUTPUT_ON_FAILURE=TRUE xvfb-run ctest -E "(knsbackend|flatpak)test"
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

backend_flatpak() {
	pkgdesc="Flatpak backend for $pkgname"
	depends=""
	install_if="$pkgname flatpak"

	amove usr/lib/qt5/plugins/discover/flatpak-backend.so
	amove usr/share/libdiscover/categories/flatpak-backend-categories.xml
	amove usr/lib/qt5/plugins/discover-notifier/FlatpakNotifier.so
	amove usr/share/applications/org.kde.discover-flatpak.desktop
	amove usr/share/metainfo/org.kde.discover.flatpak.appdata.xml
	amove usr/share/icons/hicolor/scalable/apps/flatpak-discover.svg
}

backend_apk() {
	pkgdesc="Apk backend for $pkgname"
	depends=""
	install_if="$pkgname apk-tools"

	amove usr/lib/qt5/plugins/discover/alpineapk-backend.so
	amove usr/lib/libexec/kauth/alpineapk_kauth_helper
	amove usr/share/polkit-1/actions/org.kde.discover.alpineapkbackend.policy
	amove usr/share/dbus-1/system.d/org.kde.discover.alpineapkbackend.conf
	amove usr/share/dbus-1/system-services/org.kde.discover.alpineapkbackend.service

	# JSON file contains placeholder for replacement - @CARCH@
	sed -i "s/@CARCH@/$CARCH/g" "$srcdir"/alpine-appstream-data.json
	install -Dm644 "$srcdir"/alpine-appstream-data.json \
		"$subpkgdir"/usr/share/libdiscover/external-appstream-urls/alpine-appstream-data.json

	install -Dm644 -t "$subpkgdir"/usr/share/icons/hicolor/32x32/apps/ \
		"$srcdir"/alpine-linux-logo-icon.png
}

backend_fwupd() {
	pkgdesc="fwupd backend for $pkgname"
	depends=""
	install_if="$pkgname fwupd"

	amove usr/lib/qt5/plugins/discover/fwupd-backend.so
}

sha512sums="
35e92ebc170d92267b89a99c67789732ef98c8b1847836665d3c4465be2e10a6ec39d9c53e3d6f54b9529a858ac858e64ac0db371df728750ba80b47b77528b0  discover-5.27.10.tar.xz
1ca9714ad202f4536decc99cf508f12bd1b287fba580faa2a800d5ed533649400abad544bcf1c1626b52c05d6989591d00fc02c591a76232d8c3a3ea9ee226fa  discover-apk-support.patch
d9cc12a5a92df820d6d133a3698a4fbb08a97f08f00554a11d87c73c6144b42d07cecd6682bf9576d83c8e2556e35ade2fb39e77895afbbe33e2f94630441bb7  alpine-appstream-data.json
0766668630cb14f58c840ebdda8b76bd1dec98b4dd61469677d4ad6eb4966809a25dbec2a846502cad0391df5b6ca9d692bb857a6fa203b61a52e6ac044cfbf4  alpine-linux-logo-icon.png
"
